#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <proc/readproc.h>

int main()
{
    // PROC_FILLCOM 进程名
    // PROC_FILLMEM 进程专题
    // PROC_FILLSTAT 进程状态
    PROCTAB* proc_tab = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLMEM);
    if (proc_tab == NULL) {
        perror("openproc failed");
        return EXIT_FAILURE;
    }
    proc_t proc_info;
    memset(&proc_info, 0, sizeof(proc_info));

    printf("Listing some processes:\n");
    printf("%-10s %s\n", "PID", "CMD");

    proc_t* pt_ret;
    while (1)
    {
        pt_ret = readproc(proc_tab, &proc_info);
        if (pt_ret == NULL) {
            break;
        }

        if (proc_info.cmdline != NULL) {
            // printf("%-10d %-16s, %s\n", proc_info.tid, proc_info.cmd, *proc_info.cmdline);
            printf("%-10d %-16s\n", proc_info.tid, proc_info.cmd);
        }
        else {
            printf("%-10d [%s]\n", proc_info.tid, proc_info.cmd);
        }
        freeproc(&proc_info);
        memset(&proc_info, 0, sizeof(proc_info));
    }
    closeproc(proc_tab);
    return EXIT_SUCCESS;
}
